package com.softcloud.comingforyuan.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.softcloud.comingforyuan.R;
import com.softcloud.comingforyuan.db.ExerciseDB;
import com.softcloud.comingforyuan.model.ActivityCollector;
import com.softcloud.comingforyuan.model.BaseActivity;
import com.softcloud.comingforyuan.model.Exercise;
import com.softcloud.comingforyuan.model.ExerciseAdapter;

import java.util.Calendar;
import java.util.List;


public class MistakeActivity extends BaseActivity {

    private int lastHour;
    private int lastMin;
    private int lastSec;

    private TextView titleText;
    private ListView mistakeList;
    private TextView congratulationsText;
    private Button rePractiseButton;
    private Button toMainButton;

    private List<Exercise> errorExerciseList;
    private ExerciseAdapter adapter;

    private ExerciseDB exerciseDB;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mistake);

        //设置标题
        titleText = (TextView)findViewById(R.id.title_text);
        titleText.setText("错题解析");

        //实例化控件
        mistakeList = (ListView)findViewById(R.id.wrong_list);
        congratulationsText = (TextView)findViewById(R.id.congratulations);
        rePractiseButton = (Button)findViewById(R.id.mistake_practise_mistake);
        toMainButton = (Button)findViewById(R.id.mistake_return_main);

        exerciseDB = ExerciseDB.getInstance(MistakeActivity.this);
        errorExerciseList = exerciseDB.loadWrongExercise();
//        String[] adapterContents = getContentArray();
//        adapter = new ArrayAdapter<String>(MistakeActivity.this,android.R.layout.simple_list_item_1,adapterContents);
        adapter = new ExerciseAdapter(MistakeActivity.this,R.layout.excercise_item,errorExerciseList);
        mistakeList.setAdapter(adapter);

        pref = PreferenceManager.getDefaultSharedPreferences(this);
        if(!pref.getBoolean("hasMistake",true)){
            congratulationsText.setText("恭喜你，题目全部正确！");
        }else {
            congratulationsText.setText("你有"+errorExerciseList.size()+"道题目错误");
        }

        rePractiseButton.setOnClickListener(new rePractiseButtonListener());
        toMainButton.setOnClickListener(new ToMainButtonListener());

    }

//    private String[] getContentArray(){
//        int len = errorExerciseList.size();
//        String[] res = new String[len];
//        for(int i=0;i<len;i++){
//            Exercise currExercise = errorExerciseList.get(i);
//            StringBuilder addedBuilder = new StringBuilder();
//            addedBuilder.append(currExercise.getQuestion());
//            addedBuilder.append('\n');
//            addedBuilder.append("你的答案:\n");
//            switch (currExercise.getCurrAnswer()){
//                case 1:
//                    addedBuilder.append(currExercise.getOptionA());
//                    addedBuilder.append('\n');
//                    break;
//                case 2:
//                    addedBuilder.append(currExercise.getOptionB());
//                    addedBuilder.append('\n');
//                    break;
//                case 3:
//                    addedBuilder.append(currExercise.getOptionC());
//                    addedBuilder.append('\n');
//                    break;
//                case 4:
//                    addedBuilder.append(currExercise.getOptionD());
//                    addedBuilder.append('\n');
//                    break;
//            }
//            addedBuilder.append("标准答案:\n");
//            switch (currExercise.getRightAnswer()){
//                case 1:
//                    addedBuilder.append(currExercise.getOptionA());
//                    addedBuilder.append('\n');
//                    break;
//                case 2:
//                    addedBuilder.append(currExercise.getOptionB());
//                    addedBuilder.append('\n');
//                    break;
//                case 3:
//                    addedBuilder.append(currExercise.getOptionC());
//                    addedBuilder.append('\n');
//                    break;
//                case 4:
//                    addedBuilder.append(currExercise.getOptionD());
//                    addedBuilder.append('\n');
//                    break;
//            }
//            addedBuilder.append(currExercise.getAnalyses());
//            res[i] = addedBuilder.toString();
//        }
//        return res;
//    }

    class ToMainButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MistakeActivity.this,MainActivity.class);
            startActivity(intent);
        }
    }
    class rePractiseButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(pref.getBoolean("hasMistake",true)){
                editor = pref.edit();
                editor.putBoolean("practiseMistake",true);
                editor.commit();
                Intent intent = new Intent(MistakeActivity.this,ExerciseActivity.class);
                startActivity(intent);
            }else {
                Toast.makeText(MistakeActivity.this,"暂时无错题需要练习",Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    protected void onStart() {
        super.onStart();

        errorExerciseList = exerciseDB.loadWrongExercise();
//        String[] adapterContents = getContentArray();
        adapter = new ExerciseAdapter(MistakeActivity.this,R.layout.excercise_item,errorExerciseList);
        mistakeList.setAdapter(adapter);

        if(!pref.getBoolean("hasMistake",true)){
            congratulationsText.setText("恭喜你，题目全部正确！");
        }else {
            congratulationsText.setText("你有"+errorExerciseList.size()+"道题目错误");
        }
    }

    @Override
    public void onBackPressed() {
        Calendar calendar = Calendar.getInstance();
        int currHour = calendar.get(Calendar.HOUR);
        int currMin = calendar.get(Calendar.MINUTE);
        int currSec = calendar.get(Calendar.SECOND);
        if(currHour == lastHour && currMin == lastMin && currSec-lastSec<=2){
            ActivityCollector.finishAll();
        }else{
            lastHour = currHour;
            lastMin = currMin;
            lastSec = currSec;
            Toast.makeText(this, "再次点击退出程序", Toast.LENGTH_SHORT).show();
        }
    }
}
